import java.util.Scanner;

public class Bookmanage {
	
	//定义二维数组存放用户信息（部门、用户名、密码、用户角色）
	static String[][] user =  new String[100][4];
	
	//定义二维数组存放书籍信息（编码（ISBN）、书籍名称、价格、出版社、作者）
	static String[][] book = new String[100][5];
	
	//定义二维数组存放出版社信息（出版社名称、地址、联系人）
	static String[][] pubCom = new String[100][3];
	
	//创建扫描器
	static Scanner scanner = new Scanner(System.in);
	
	public static void main(String[] args) {
		//调用初始化数据的方法
		init();
		
		//调用登录功能
		login();
	}

	
	//系统登录功能
	private static void login() {
		// TODO Auto-generated method stub
		//循环，使用户输错密码可以重新输入用户名和密码
		while(true) {
			System.out.println("*********欢迎使用图书管理系统*************");
			System.out.println("请输入用户名：");
			String userid = scanner.next();
			System.out.println("请输入密码：");
			String password = scanner.next();
			
			//调用方法，传入参数(用户名,密码)；根据用户名和密码查找用户
			//定义变量find存放方法的返回值,找到为true,没有找到为false
			boolean find = findUser(userid,password);
			
			//判断返回值find
			if(find) {
				//find为true，用户登录成功，进入系统菜单
				//调用系统功能菜单方法
				manageMenu();
				
			}
			else {
				//find为false，用户名和密码错误重新输入
				System.out.println("该用户不存在或者密码错误！请重新登录！");
			}
		}
		
	}

	
	//系统功能方法
	private static void manageMenu() {
		// TODO Auto-generated method stub
		while(true) {
			System.out.println("1 图书管理  2 出版社管理  3 退出系统");
			System.out.println("请输入数字选择功能：");
			int c = scanner.nextInt();
			
			//判断用户选择的数字，进入不同的管理界面或退出系统
			if(c == 1) {
				//1 图书管理 
				bookManege();
			}
			else if(c == 2) {
				//2 出版社管理
				pressManege();
				
			}
			else if(c == 3) {
				//3 退出系统
				System.exit(0);
			}
			else {
				System.out.println("选择错误,请重新选择");
			}
		}
		
	}


	//出版社管理模块
	private static void pressManege() {
		// TODO Auto-generated method stub
		boolean vlog =true ;
		while (vlog) {
			System.out.println("请输入：1.增加  /t 2.删除  /t 3.更新  /t 4.根据出版社名称查询 /t 5.查询所有出版社 /t 6.返回上一级菜单");
			System.out.println("请输入数字选择功能：");
			int sum = scanner.nextInt();
			
			if (sum == 1) {
				//图书增加
				addBook1();
			}else if(sum == 2) {
				//图书删除
				deleteBook1();
				}
			 else if(sum == 3) {
				//更新图书
				updateBook1();
				}
			 else if(sum == 4) {
				//查询
				selectBook1();
				}
			 else if(sum == 5) {
				//返回上一级
				vlog = false;
			}else {
					System.out.println("选择错误，请重新选择");
			}
			
		}
		}
	private static void selectBook1() {
		// TODO Auto-generated method stub
		System.out.println("请输入要更新的出版社名称：");
		String press= scanner.next();
		System.out.println("请输入要更新的出版社地址：");
		String pressaddress =scanner.next();
		System.out.println("请输入要更新的出版社联系人：");
		String person =scanner.next();
		
		boolean selectBook1 =book3(press,pressaddress,person);
		for (int i = 0; i < book.length; i++) {
			if (selectBook1) {
				System.out.println("查询成功");
			}else {
				System.out.println("查询失败");
			}
		}
	}


	private static boolean book3(String press, String pressaddress, String person) {
		// TODO Auto-generated method stub
		boolean selectBook1=false;
		for (int i = 0; i < book.length; i++) {
			if (pubCom.equals(pubCom[i][0])) {
				selectBook1=true;
			}
		}
		return selectBook1;
	}


	private static void updateBook1() {
		// TODO Auto-generated method stub
		System.out.println("请输入要更新的出版社名称：");
		String press= scanner.next();
		System.out.println("请输入要更新的出版社地址：");
		String pressaddress =scanner.next();
		System.out.println("请输入要更新的出版社联系人：");
		String person =scanner.next();
		
		boolean updateBook1 =book2(press,pressaddress,person);
		for (int i = 0; i < book.length; i++) {
			if (updateBook1) {
				System.out.println("添加成功");
			}else {
				System.out.println("添加失败");
			}
		}
				
	}


	private static boolean book2(String press, String pressaddress, String person) {
		// TODO Auto-generated method stub
		boolean updateBook1 =false;
		for (int i = 0; i < book.length; i++) {
			if (pubCom.equals(pubCom[i][0])) {
				updateBook1=true;
			}
		}
		return updateBook1;
	}


	private static void deleteBook1() {
		// TODO Auto-generated method stub
		System.out.println("请输入要删除的出版社名称：");
		String press =scanner.next();
		System.out.println("请输入要删除的出版社地址：");
		String pressaddress= scanner.next();
		System.out.println("请输入出版社联系人：");
		String person =scanner.next();
		
		boolean deleteBook1 =book1(press,pressaddress,person);
		for (int i = 0; i < book.length; i++) {
			if(deleteBook1) {
				System.out.println("删除成功");
			}else {
				System.out.println("未成功删除，请重新输入");
			}
		}
		
	
	}


	private static boolean book1(String press, String pressaddress, String person) {
		// TODO Auto-generated method stub
		boolean deleteBook1 =false;
		for (int i = 0; i < book.length; i++) {
			if (pubCom.equals(pubCom[i][0])) {
				deleteBook1=true;
			}
		}
		
		return true;
	}


	private static void addBook1() {
		// TODO Auto-generated method stub
		System.out.println("请输入出版社名称：");
		String press =scanner.next();
		System.out.println("请输入出版社地址：");
		String pressaddress =scanner.next();
		System.out.println("请输入出版社联系人：");
		String person =scanner.next();
		
		boolean addBook1 = book(press,pressaddress,person);
		for (int i = 0; i < book.length; i++) {
			if (addBook1 ) {
				System.out.println("出版社添加成功");
			}else {
				System.out.println("出版社添加失败");
			}
		}
	}
	


	private static boolean book(String press, String pressaddress, String person) {
		// TODO Auto-generated method stub
	  boolean  addBook1 =false;
		
		for (int i = 0; i < pubCom.length; i++) {
			if(pubCom.equals(pubCom[i][0])) {
				addBook1 = true;
				break;
			}
		}
		
		return addBook1 ;
	}
	


	//图书管理模块菜单
	private static void bookManege() {
		// TODO Auto-generated method stub
		boolean flag = true;  //定义变量存放菜单循环的条件，使选择返回上级菜单时，flag=false，退出当前循环
		
		//循环输出图书管理模块菜单
		while(flag) {
			System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
			System.out.println("请输入数字选择功能：");
			int num = scanner.nextInt();
			
			if(num == 1) {
				//图书增加
				addBook();
			}else if(num == 2) {
				//图书删除
				deleteBook();
				}
			 else if(num == 3) {
				//更新图书
				updateBook();
				}
			 else if(num == 4) {
				//查询
				selectBook();
				}
			 else if(num == 5) {
				//返回上一级
				flag = false;
			}else {
					System.out.println("选择错误，请重新选择");
			}
			
		}
		
	}

    // 查询菜单
	private static void selectBook() {
		
		boolean flag = true;
		
		while(flag) {
			System.out.println(	"		1. 根据ISBN查询\r\n" + 
								"		2. 根据书名查询（模糊）\r\n" + 
								"		3. 根据出版社查询\r\n" + 
								"		4. 根据作者查询\r\n" + 
								"		5. 根据价格范围查询\r\n" + 
								"		6. 查询所有书籍信息\r\n" + 
								"		7. 返回上一级菜单"
							  );
			
			System.out.println("请输入数字选择功能：");
			int num1 = scanner.nextInt();
			
			if(num1 == 1) {
				//1. 根据ISBN查询
				searchByIsbn();
			}
			else if (num1 == 2) {
				//2. 根据书名查询（模糊）
				searchByName();
		    }
			else if (num1 == 3) {
				//3. 根据出版社查询
				searchByPress();
			}
			else if (num1 == 4) {
				//4. 根据作者查询
				searchByEditor();
			}
			else if (num1 == 5) {
				//5. 根据价格范围查询
				searchByPrice();
			}
			else if (num1 == 6) {
				//6. 查询所有书籍信息
				searchAllBook();
			}
			else if (num1 == 7) {
				//返回上一级
				flag = false;
			}
			else {
			    System.out.println("选择错误，请重新选择");
			}
		}
	}
	//根据作者查询
	private static void searchByEditor() {
		// TODO Auto-generated method stub
		System.out.println("请输入要查询的作者名称：");
		String editor =scanner.next();
		
		for (int i = 0; i < book.length; i++) {
			if (editor.equals(book[i][4])) {
				printIndexBook(i);
			}
			else {
				System.out.println("输入有误,请重新输入");
			}
		}
	}

	//根据出版社查询
	private static void searchByPress() {
		// TODO Auto-generated method stub
		System.out.println("请输入要查询的出版社名称：");
		String press =scanner.next();
	
		for (int i = 0; i < book.length; i++) {
			if (press.equals(book[i][3])) {
				printIndexBook(i);
			}
			else {
				System.out.println("请重新输入出版社名称");
			}
		}
		
	}


	//根据价格查询书籍信息
	private static void searchByPrice() {
		// TODO Auto-generated method stub
		System.out.println("请输入最低价格：");
		int min = scanner.nextInt();
		System.out.println("请输入最高价格：");
		int max = scanner.nextInt();
		
		
		for (int i = 0; i < book.length; i++) {
			if(book[i][0]!=null) {
				//Integer.parseInt(string str)将字符串转换成整型
				int price =Integer.parseInt(book[i][2]);
				//书籍的价格book[i][2]>=min  && book[i][2]<=max
				if(price>=min  && price<=max) {
					printIndexBook(i);
				}
			}else {
				break;
			}
		}
		
	}


	//根据书名模糊查询
	private static void searchByName() {
		// TODO Auto-generated method stub
		System.out.println("请输入要查询的书籍名称：");
		String bookName = scanner.next();
		System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
		//循环遍历book数组
		for(int i=0; i < book.length ; i++) {
			
			//判断第i本书的书名book[i][1]是否与用户输入的书名匹配
			if(book[i][1]!=null && book[i][1].indexOf(bookName)!=-1) {
				printIndexBook(i);
			}
		}
	}

	//打印输出第index行的书籍信息
	public static void printIndexBook(int index) {
		for(int i =0 ; i < book[index].length; i++) {
			System.out.print(book[index][i]+"\t");
		}
		System.out.println();
	}
	

	//根据isbn查询书籍信息
	private static void searchByIsbn() {
		// TODO Auto-generated method stub
		
		System.out.println("请输入要查询的isbn");
		String isbn = scanner.next();
		
		int index = getIndexByISBN(isbn);
		System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
		//打印输出第index行的书籍信息
		printIndexBook(index);
		
		System.out.println();
	}

	
	//修改书籍信息
	private static void updateBook() {
		// TODO Auto-generated method stub
		searchAllBook();
		System.out.println("请输入要更新的书籍的isbn编码：");
		String isbn = scanner.next();
		
		//调用方法获取isbn所在的下标值；
		int index = getIndexByISBN(isbn);
		
		//如果index不为-1，找到要更新的书籍行下标
		if(index != -1) {
			//将用户输入的更新后的书籍信息存放到第index行的书籍信息中
			System.out.println("请输入更新的书名:");
			book[index][1] = scanner.next();
			System.out.println("请输入更新的价格:");
			book[index][2] = scanner.next();
			System.out.println("请输入更新的出版社:");
			book[index][3] = scanner.next();
			System.out.println("请输入更新的作者:");
			book[index][4] = scanner.next();
			//更新后显示所有的书籍信息
			searchAllBook();
			
		}else {
			System.out.println("更新失败，没有要更新的isbn编码的书籍");
		}
		
	}


	//删除书籍信息
	private static void deleteBook() {
		// TODO Auto-generated method stub
		searchAllBook();
		System.out.println("请输入要删除的书籍的isbn编码：");
		String isbn = scanner.next();
		
		//调用方法获取isbn所在的下标值；
		int index = getIndexByISBN(isbn);
		
		//index不等于-1，index就是要删除的isbn书籍的行下标；index=-1，没有找到要删除的isbn信息
		if(index != -1) {
			//删除：循环将后面的书籍数组元素往前移
			for (int i = index; i < book.length-1; i++) {
				
				//后面的书籍信息赋值给前面的数组中
				book[i] =book[i+1];
				
				//如果后面的元素都为空，就不用再继续循环
				if(book[i+1] == null ) {
					break;
				}
			}
			//将数组的最后一个元素设置为null;预防最后一个书籍数组元素有数据
			if(book[book.length-1][0] != null) {
				book[book.length-1][0] = null;
				book[book.length-1][1] =null;
				book[book.length-1][2] =null;
				book[book.length-1][3] =null;
				book[book.length-1][4] =null;
			}
			
			System.out.println("删除成功");
			searchAllBook();
		
		}
		else {
			System.out.println("没找到要删除的书籍 信息");
		}
		
		
	}
	
	//查询显示所有的书籍信息
	public static void searchAllBook() {
		System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
		for (int i = 0; i < book.length; i++) {
			//System.out.println(book[i][0]+"\t"+book[i][1]+"\t"+book[i][2]+"\t"+book[i][3]+"\t"+book[i][4]);
			if(book[i][0]!=null) {
				for (int j = 0; j < book[i].length; j++) {
					System.out.print(book[i][j]+"\t");
					
				}
				System.out.println();
			}
			
		}
	}

	//增加图书方法
	private static void addBook() {
		// TODO Auto-generated method stub
		//1.接受用户输入的书籍信息
		System.out.println("请输入图书ISBN:");
		String isbn = scanner.next();
		System.out.println("请输入书名:");
		String bookName = scanner.next();
		System.out.println("请输入价格:");
		String bookPrice = scanner.next();
		System.out.println("请输入出版社:");
		String pubCom = scanner.next();
		System.out.println("请输入作者:");
		String author = scanner.next();
		
		//调用查找isbn在书籍数组中的下标位置的方法；
		int index = getIndexByISBN(isbn);
		
		//如果index==-1代表没有找到isbn，就能进行增加操作
		if(index == -1) {
			//调用方法获取书籍数组中第一个null的下标值；确定将书籍保存到书籍数组中的哪个位置
			int local = getIndexFirstNUll(); 
			
			//如果local不等于-1,找到插入数组的位置local
			if(local != -1) {
				//调用方法将书籍信息保存到数组元素中
				save(isbn,bookName,bookPrice,pubCom,author,local);
				System.out.println("新增成功");
				searchAllBook();
				
			}
			else {
				System.out.println("书籍数组已满，没有位置插入");
			}
			
		}
		else {
			System.out.println("isbn编码的图书已存在，不能新增");
		}
		
		
		
	}

	//将书籍信息保存到数组元素中,保存的行下标为local
	private static void save(String isbn, String bookName, String bookPrice, String pubCom, String author,int local) {
		// TODO Auto-generated method stub
		
		book[local][0] = isbn;
		book[local][1] = bookName;
		book[local][2] = bookPrice;
		book[local][3] = pubCom;
		book[local][4] = author;
	
		
	}

	private static int getIndexFirstNUll() {
		// TODO Auto-generated method stub
		int index = -1;
		for (int i = 0; i < book.length; i++) {
			if(book[i][0] == null) {
				index = i;
				break;
			}
		}
	
		return index;
	}

	private static int getIndexByISBN(String isbn) {
		// TODO Auto-generated method stub
		int index = -1;
		
		for (int i = 0; i < book.length; i++) {
			if(isbn.equals(book[i][0])) {
				index = i;
			}
		}
		
		return index;
	}


	
	private static boolean findUser(String userid, String password) {
		// TODO Auto-generated method stub
		boolean find = false;
		
		for(int i = 0; i < user.length; i++) {
			if(userid.equals(user[i][1]) && password.equals(user[i][2])) {
				
				find = true;
				
				break;
			}
			
		}

		return find;
	}


	//初始化系统基础数据
	private static void init() {
		// TODO Auto-generated method stub
		//初始化数据
				//第1个用户信息：行下标 0
				user[0][0] = " HP001"; //部门
				user[0][1] = "ken";//用户名
				user[0][2] ="123";//密码
				user[0][3] ="admin";//角色
				
				//第2个用户信息：行下标 1
				user[1][0] = " HP001"; //部门
				user[1][1] = "ken";//用户名
				user[1][2] ="123";//密码
				user[1][3] ="admin";//角色
				
				
				user[2][0] = "HP003";
				user[2][1] = "guile";
				user[2][2] = "123";
				user[2][3] = "user";

				//书籍信息初始化
				//第1个书籍信息：行下标 0
				book[0][0] = "001";			//isbn编码
				book[0][1] = "JAVA高级编程";	//书籍名称
				book[0][2] = "100";			//价格
				book[0][3] = "铁道部出版社";		//出版社
				book[0][4] = "张三";			//作者

				//第2个书籍信息：行下标 1
				book[1][0] = "002";
				book[1][1] = "SQLSERVER高级编程";
				book[1][2] = "150";
				book[1][3] = "清华出版社";
				book[1][4] = "李四";

				//第3个书籍信息：行下标 2
				book[2][0] = "003";
				book[2][1] = "JAVA基础入门";
				book[2][2] = "50";
				book[2][3] = "铁道部出版社";
				book[2][4] = "张三";

				book[3][0] = "004";
				book[3][1] = "ORACLE高级编程";
				book[3][2] = "120";
				book[3][3] = "清华出版社";
				book[3][4] = "王五";

				//出版社信息初始化
				//第1个出版社信息：行下标 0
				pubCom[0][0] = "清华出版社";  	//出版社名称
				pubCom[0][1] = "武汉市XXXX";	//地址
				pubCom[0][2] = "aaa";		//联系人

				//第2个出版社信息：行下标 1
				pubCom[1][0] = "铁道出版社";
				pubCom[1][1] = "北京市XXXX";
				pubCom[1][2] = "bbb";

				pubCom[2][0] = "邮电出版社";
				pubCom[2][1] = "南京市XXXX";
				pubCom[2][2] = "ccc";
		
	}
	
}


